package com.caits.lbs.framework.services.sms;

import org.apache.log4j.Logger;
import org.springframework.util.Assert;

import com.caits.lbs.framework.log.CommonLogFactory;
import com.caits.lbs.framework.utils.ClassUtils;
import com.caits.lbs.framework.utils.StringUtils;
import com.royasoft.mas.api.SMSAgent;
import com.royasoft.mas.api.model.SMSSubmitMessage;

/**
 * <p>
 * MessageServiceSMSImpl
 * </p>
 * <p>
 * 用途：消息服务的，短信sms实现
 * </p>
 * 
 * @author 周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 * @version 0.0.1 2011-8-10
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">版本号</th>
 *          <th width="100px">动作</th>
 *          <th width="100px">修改人</th>
 *          <th width="100px">修改时间</th>
 *          </tr>
 *          <!-- 以 Table 方式书写修改历史 -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2011-8-10 下午5:38:35</td>
 *          </tr>
 *          <tr>
 *          <td>0.0.1</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2011-8-10 下午5:38:35</td>
 *          <td>0.0.2</td>
 *          <td>修改类</td>
 *          <td>xxx</td>
 *          <td>x年x月x日</td>
 *          </tr>
 *          </table>
 */
public class MessageServiceSMSImpl implements IMessageService {

	/** 日志记录器 */
	private Logger log = CommonLogFactory.getLog();

	/** 应用id */
	private static final String applicationID = "P000000000008002";

	/** 短信用配置文件路径 */
	private static String path;

	/** 实际消息发送载体 */
	private SMSSubmitMessage message;

	/**
	 * 构造器
	 */
	public MessageServiceSMSImpl() {

	}

	/**
	 * 初始化配置文件的路径，并构造消息实例
	 */
	protected void init() {
		SMSAgent.initialize(ClassUtils.getResourceFromClassPath(path));
		message = new SMSSubmitMessage(java.util.UUID.randomUUID().toString(),
				null, null, applicationID);
		message.setReqDeliveryReport(0);
	}

	/**
	 * 发送测试
	 * 
	 * @param strPhone
	 * @param msg
	 */
	public void sendTest(String strPhone, String msg) {
		SMSSubmitMessage message = new SMSSubmitMessage(java.util.UUID
				.randomUUID().toString(), strPhone, msg, applicationID);
		message.setReqDeliveryReport(0);

		for (int i = 0; i < 3; i++) {
			try {
				Thread.sleep(1000);
				SMSAgent.sendSMS(message);
				log.info("sender " + strPhone + " ok!");
				break;
			} catch (Exception e) {
				log.error("sender err:" + e.getMessage());
			}
		}
	}

	@Override
	public void sendMessage(IMessage msg) {
		Assert.notNull(msg, "消息对象不能为空");

		try {
			String[] toAddress = StringUtils.splitToArray(msg.getHead(), ",",
					true);
			for (String address : toAddress) {
				message = new SMSSubmitMessage(java.util.UUID.randomUUID()
						.toString(), null, null, applicationID);
				message.setReqDeliveryReport(0);
				message.setContent(msg.getBody());
				message.setDestAddr(address);
				SMSAgent.sendSMS(message);
				log.info("消息已发送,to" + address);
			}
		} catch (Exception e) {
			log.error("消息发送失败" + e.getLocalizedMessage());
		}

	}

	/**
	 * 获取变量<code>path</code>的值
	 * 
	 * @return 返回的数据类型是<code>String</code>
	 */
	public String getPath() {
		return path;
	}

	/**
	 * 设置变量<code> path</code> 的值
	 * 
	 * @param path
	 *            <code>path</code> 参数类型是<code>String</code>
	 */
	public void setPath(String path) {
		MessageServiceSMSImpl.path = path;
	}

	@Override
	public void registerConsumer(IConsumeMessage consumer) {
		log.warn("暂未实现消息接收.");
	}
	/**
	 * 开始接收消息 
	 */
	public void startListen() {
		log.warn("暂未实现消息接收.");
	}
	/**
	 * 停止接收消息 
	 */
	public void stopListen() {
		log.warn("暂未实现消息接收.");
	}
}